using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SkillupdatePanel2 : BasePanel
{
    static readonly string path = "UI/Panel/SkillupdatePanel2";
    static readonly string skillpath = "UI/Skill/skill";
    static readonly string panelpath = "UI/SkillinPanel/";
    static readonly string playerpath = "UI/SkillinPlayer/";
    public SkillupdatePanel2() : base(new UIType(path)) { }
    public int tempnum;
    public static int skilllevel;
    private void updateskill(int num)
    {
        tempnum = num;
        uitool.GetOrAddComponentInChildren<Button>(PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[num] + "(Clone)").enabled = false;
        uitool.GetOrAddComponentInChildren<Button>(PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[1-num] + "(Clone)").interactable = false;
    }
    public void remakeskill(int num)
    {
        tempnum = -1;
        if (num == -1)
            return;
        uitool.GetOrAddComponentInChildren<Button>(PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[num] + "(Clone)").enabled = true;
        uitool.GetOrAddComponentInChildren<Button>(PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[1 - num] + "(Clone)").interactable = true;
    }
    public override void OnEnter()
    {
        tempnum = -1;
        if (PlayerData.updateextranames[0][0] == '2')
            skilllevel = 2;
        else skilllevel = 1;
        for (int i = 0; i < 2; i++)
        {
            Debug.Log(PlayerData.updateextranames[i]);
            GameObject.Instantiate(Resources.Load<GameObject>(panelpath + PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[i]), GameObject.Find("skillss").transform);
            Debug.Log(PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[i]);
        }

        uitool.GetOrAddComponentInChildren<Button>(PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[0] + "(Clone)").onClick.AddListener(() =>
        {
            if(PlayerData.coinsforskill >= skilllevel)
                updateskill(0);
            

        });
        uitool.GetOrAddComponentInChildren<Button>(PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[1] + "(Clone)").onClick.AddListener(() =>
        {
            if (PlayerData.coinsforskill >= skilllevel)
                updateskill(1);
        });
        uitool.GetOrAddComponentInChildren<Button>("return").onClick.AddListener(() =>
        {
            panelmanager.PopAndPush(new SkillupdatePanel());
        });
        uitool.GetOrAddComponentInChildren<Button>("remake").onClick.AddListener(() =>
        {
            remakeskill(tempnum);
        });
        uitool.GetOrAddComponentInChildren<Button>("sure").onClick.AddListener(() =>
        {
            if (tempnum != -1)
            {
                PlayerData.extranames[PlayerData.updatenamenum] = PlayerData.updateextranames[tempnum];
                PlayerData.skillinplayer[PlayerData.updatenamenum] = Resources.Load<GameObject>(playerpath + PlayerData.skillnames[PlayerData.updatenamenum] + PlayerData.updateextranames[tempnum]);
                PlayerData.coinsforskill -= skilllevel;
                panelmanager.PopAndPush(new SkillupdatePanel());
            }
        });
    }
}
